﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Test_QianBZ.Domain;
using Test_QianBZ.Model;
using Test_QianBZ.Models;

namespace Test_QianBZ.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index(int personId = 0)
        {
            ViewBag.personId = personId;
            var eatlog = new EatlogDomain();
            ViewBag.LastEat = eatlog.GetLastEat(personId);
            ViewBag.NearEat = eatlog.GetLogWithName(personId).OrderByDescending(s => s.EatTime).ToList();
            ViewBag.Menu = new FoodDomain().GetMenu().OrderByDescending(s=>s.FoodId).ToList();
            ViewBag.Person = new PersonDomain().GetAllPerson().OrderByDescending(s => s.PersonId).ToList();
            return View();
        }

        public IActionResult DelEatLog(int LogId = 0)
        {
            if(new EatlogDomain().DelEatLog(LogId) > 0)
            {
                return Json("删除成功");
            }
            else
            {
                return Json("删除失败");
            }
        }

        public IActionResult EditFood(FoodModel food)
        {
            if (new FoodDomain().EditFood(food) > 0)
            {
                return Json("修改成功");
            }
            else
            {
                return Json("修改失败");
            }
        }

        public IActionResult AddPerson(string personName = "")
        {
            if (personName == "") return Json("姓名不能为空");
            return Json(new PersonDomain().AddPerson(personName)>0?"成员插入成功":"成员插入失败");
        }

        public IActionResult AddFood(string foodName = "")
        {
            if (foodName == "") return Json("菜名不能为空");
            return Json(new FoodDomain().AddFood(foodName) > 0 ? "菜品插入成功" : "菜品插入失败");
        }

        public IActionResult AddEatLog(EatLogModel eatLog)
        {
            if (eatLog.EatTime == null) eatLog.EatTime = DateTime.Now;
            if (eatLog.EatCount == null || eatLog.EatCount <= 0)return Json("数量不能小于零");
            if (eatLog.FoodId <= 0)return Json("必须选择菜肴");
            if (eatLog.PersonId <= 0)return Json("必须选择人");
            return Json(new EatlogDomain().AddLog(eatLog)>0? "记录插入成功": "记录插入失败");
            //if(eatLog.EatCount = )
            //return Json("");
        }
        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
